package me.baicai.weixin.provider.utils;

import me.baicai.weixin.entity.gcb.Device;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


@Component
public class MD5Algorithm {


    @Value(value = "${gcb.key}")
    private String key;


    private final String optionA = "11111";
    private final String optionB = "22222";
    private final String optionC = "33333";
    private final String optionD = "44444";
    private final String optionE = "55555";

    public String passwordCalculation(String sn, Device step, int key){
        String dev =  this.key + sn + (String.format("%02d",step.getStep()));
        byte [] array = this.MD5Byte(dev);
        String str = "";
        str += ( array[14] & 0x3 ) +1;
        str += ( (array[15]>>6) & 0x3 ) +1;
        str += ( (array[15]>>4) & 0x3 ) +1;
        str += ( (array[15]>>2) & 0x3 ) +1;
        str += ( (array[15]) & 0x3 ) +1;
        if(str.equalsIgnoreCase(this.optionA) || str.equalsIgnoreCase(this.optionB)
                || str.equalsIgnoreCase(this.optionC) || str.equalsIgnoreCase(this.optionD)
                || str.equalsIgnoreCase(this.optionE) ){
            if (step.getStep() == 20) {
                step.setStep(0);
            }
            step.setStep(step.getStep()+1);
            this.passwordCalculation(sn,step,key);
        }
        if (step.getStep() == 20) {
            step.setStep(0);
        }
        step.setStep(step.getStep()+1);
        return key+str;
    }

    public String bluetoothPasswd(String sn, int time){
        /*String key = "SZWS01";
        String sh = "01";
        String tmp = StringUtils.leftPad(Integer.toHexString(time).toUpperCase(), 4, '0');
        return "F00601"+tmp+ MD5(key + sn + sh + tmp).substring(0,6);*/

        String key = "LGW";
        String sh = "01";
        String tmp = StringUtils.leftPad(Integer.toHexString(time).toUpperCase(), 4, '0');
        return "F00601"+tmp+ MD5(key + sn + sh + tmp).substring(0,6);
    }

    private String MD5(String key) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        try {
            byte[] btInput = key.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;

            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }


    private static byte[]  MD5Byte(String key) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        byte[] btInput = key.getBytes();
        // 获得MD5摘要算法的 MessageDigest 对象
        MessageDigest mdInst = null;
        try {
            mdInst = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        // 使用指定的字节更新摘要
        mdInst.update(btInput);
        // 获得密文
        return mdInst.digest();
    }


}
